/**
 * 文档列表模块，提供列表功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto","docdetailEdit"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto,docdetailEdit) {

	'use strict';

	var    $typedg,$typegrid,$detaildg,$detailgrid,$edit_window,_defaultConfig,listTd,
			$panel,_bizProto,_moduleConfig,_buttonConfig,$form,			
			docdetailEditUrl,
			doctypeQryRoot = '/doctype/query/getList.action',
			doctypeQryUrl='/doctype/query/getTreeList.action',
			docdetailQryUrl='/document/query/getTypeListDomain.action',
			docdetailSaveUrl='/docdetail/save.action',
			docdetailDelUrl='/docdetail/del.action'
		   	
    
	var	private_method = {
		init : function(_options) {
			
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			
			if (window.console) {
				console.info(_moduleConfig.fullBizcode +' modules gets ready and been loaded.');
			}
			
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:'sysman',
				moduleCode:'docdetail',
				btnSelector:'div.doc-detail-list-btn',
				doccode:null,
				edit_window:null,
				onWindowClose:null, //window close event
				initialized:false, //初始化标志
				conditon_param:null,//初始化明细列表查询参数
				//初始化元素外部扩展函数
				initElement:null
			}
					
			_defaultConfig = $.extend({},_defaultConfig,_moduleConfig,_options)	
			_defaultConfig.moduleCode = _defaultConfig.bizcode
			_bizProto = new bizProto(_defaultConfig);	
			
			_bizProto.gridHeight -= 15
			
			_defaultConfig.bizProto = _bizProto
			$edit_window = $('#win-doc-manage');
				
			_buttonConfig={
				"新增":private_method.add,
			    "编辑":private_method.edit,
				"删除":private_method.del,
				"清除选择":private_method.clear,
				"批量提交":function(evt){private_method.auditList({status:0,evt:evt})},
				"批量审核":function(evt){private_method.auditList({status:1,evt:evt})},
				"批量退回":function(evt){private_method.auditList({status:-1,evt:evt})}			 								
			}
			
			
			_defaultConfig.initialized = true;
			sysmanMain.setCurrentModuConfig(_defaultConfig);
			
			private_method.initElement(_bizProto.pageDoc)
		},
		getCurrentPageEl:function(){
			var _config = sysmanMain.getCurrentModuConfig();
			var tab = sysmanMain.getCurrentTab();
			//按照id查找
			var doc = $(tab).find('#doc-'+_config.bizcode)
			
			if(doc.length<=0){
				doc = $(tab).find('div.doc-manage')
				if(doc.length>0){
					$(doc).attr('id','doc-'+_config.bizcode);
				}
			}
			
			listTd = $(doc).find(_defaultConfig.btnSelector)
			
				$typedg = $(doc).find("table.dg-type");
			$typegrid = $(doc).find('#dg-doctype-'+ _config.bizcode)
			if($typegrid.length<=0){
				$typegrid = $(doc).find("table.dg-type");
				$typegrid.attr('id','dg-doctype-'+ _config.bizcode)
			}
			
			
				$detaildg = $(doc).find("table.dg-detail");
			$detailgrid = $(doc).find('#dg-docdetail-'+ _config.bizcode)
			if($detailgrid.length<=0){
				$detailgrid = $(doc).find("table.dg-detail");
				$detailgrid.attr('id','dg-docdetail-'+ _config.bizcode)
			}		
			
			_bizProto.pageDoc = doc
		},
		
		initElement: function (doc) {
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			
			//初始化操作按钮
			
			
			var _param = {
				bizcode:_moduleConfig.fullBizcode,
				rootPath:_bizProto.rootPath,
				container:listTd,
				callback:private_method.bindBtnEvent}
			btnServ.generateListBtn(_param);
			
			
			//判断类型列表是否存在
			if($typegrid && $typegrid.length==1){
				var typeParam = {
					height : _bizProto.gridHeight,
					url : _bizProto.rootPath + doctypeQryRoot,
					onClickRow:private_method.getDocdetail
				}
				
				if(_moduleConfig.typeUrl){
					typeParam.url = _bizProto.rootPath + _moduleConfig.typeUrl
				}
				
				if(_moduleConfig.addition_param){
					var _code = $.parseJSON(_moduleConfig.addition_param)
					if(_code.typeid){
						typeParam.queryParams={id:_code.typeid}
					}				
				}				
			
				$typegrid=$($typedg).treegrid(typeParam);
				$typegrid.treegrid('options').url =  _bizProto.rootPath + doctypeQryUrl;				
			}
			
			
			//判断明细列表是否存在
			if($detailgrid && $detailgrid.length==1){
				var _detailParam = {
					height :_bizProto.gridHeight,
					url : _bizProto.rootPath + docdetailQryUrl,
					toolbar:$(listTd)
				}				
				//模块初始化查询参数
				if(_moduleConfig.condition_param){
					_detailParam.queryParams = _moduleConfig.condition_param
				}			
				
				if(_moduleConfig.queryMethod){
					_detailParam.url = _bizProto.rootPath + '/document/query/'+_moduleConfig.queryMethod+'.action'
				}
				
				//配置数据加载预处理函数
				if(_moduleConfig.detailLoadFilter){
					_detailParam.loadFilter = _moduleConfig.detailLoadFilter
				}
				
				//双击事件				
				_detailParam.onDblClickRow = private_method.detailDblClick
				
				$detailgrid=$($detaildg).datagrid(_detailParam);
			}
			//初始化元素函数
			if(_moduleConfig.initElement){
				var _initElementParam={doc:doc,rootId:_moduleConfig.rootId}
			
				$form = $(doc).find(_moduleConfig.conditionForm)
				if($form.length>0){
					_initElementParam.form = $form
				}
				_moduleConfig.initElement(_initElementParam)
			}
			$.messager.progress('close');
			
		},		
		
		 getDocdetail:function (rowData){
		 	private_method.getCurrentPageEl();
		 	$detailgrid.datagrid('load',{condition:{typeId:rowData.id}})
		 	var _moduleConfig = sysmanMain.getCurrentModuConfig();
		 	_moduleConfig.typeid = rowData.id;
		 	_moduleConfig.typeRow = rowData;
		 	sysmanMain.setCurrentModuConfig(_moduleConfig);
		},
		//绑定按钮事件
		bindBtnEvent:function(){
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			// 绑定按钮事件
			var btn = $(_moduleConfig.bizProto.pageDoc).find('div.doc-detail-list-btn a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		// 新增函数
		add : function(evt) {
			private_method.getCurrentPageEl();
			var _moduleConfig = sysmanMain.getCurrentModuConfig();					
			if (!_moduleConfig.typeid) {					
				msgServ.Msgslide('请选择类型');
				return
			}
			$($detailgrid).datagrid('clearSelections')
			private_method.editExecute(false,evt);
		},
		// 编辑,flag 为form按钮可见性标志
		edit : function(evt) {	
			private_method.editExecute(true,evt);				
		},
		editExecute : function(flag,evt) {	
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			var _param = {grid:$detailgrid,window:$edit_window,flag:flag}
			if(_moduleConfig.onWindowClose){
				_param.onClose=_moduleConfig.onWindowClose
			}
			
			_param.typeid = _moduleConfig.typeid
			docdetailEdit.pageShow(_param)	
			if(evt && evt.currentTarget){
				$(evt.currentTarget).linkbutton('enable')
			}
		},
		
		/**明细列表双击事件
		 * @param _options.index	选择行序号
		 * @param _options.row		选中行
		 * @param _options.type	0,缺省编辑方法;1,外部编辑方法
		 */
		detailDblClick:function(index,row){
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			var _param = {grid:$detailgrid,window:$edit_window,flag:false,data:row}
			private_method.getCurrentPageEl();
			$detailgrid.datagrid('clearSelections');
			$detailgrid.datagrid('selectRow',index);
			if(!_moduleConfig.rowDblClick)
				private_method.editExecute();
			else{
				_moduleConfig.rowDblClick(_param)
			}
				
		},
		
		
		// 删除
		del : function(evt) {
			private_method.getCurrentPageEl();
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			if (window.console) {
			console.info(_moduleConfig.fullBizcode +' delete on air ...');
			}
			var _param = {url:_moduleConfig.bizProto.rootPath + docdetailDelUrl,target:$detailgrid}
			if(_moduleConfig.auditEnable){
				_param.	auditEnable = _moduleConfig.auditEnable
			}
			_param.evt = evt;
			datagridServ.delRow(_param);
		},
		
		/**
		 * 
		 * 批量审核
		 * @param _options.status	审核状态
		 * 
		 */ 
		auditList : function(_options) {
			if(window.console){
				
				console.log('del event fired ...')
			}
			var _config = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			
			_bizProto = _config.bizProto
			datagridServ.auditList({url:_bizProto.auditListUrl,grid:$detailgrid,status:_options.status,evt:_options.evt});
			
		},
		clear : function(evt) {	
			private_method.getCurrentPageEl();
			$($detailgrid).datagrid('clearSelections')
			if(evt && evt.currentTarget){
				$(evt.currentTarget).linkbutton('enable')
			}
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		}
	}	
	return public_method;
});